import json
import os
from data.config import PREPROCESSED_DATA_DIR
from utils.parse_json import load_json_file, load_jsonl_file
from utils.rag_utils import cal_content_embedding


all_requirements = load_json_file(os.path.join(PREPROCESSED_DATA_DIR, "all_requirements.json"))
all_test_cases = load_jsonl_file(os.path.join(PREPROCESSED_DATA_DIR, "all_test_cases.jsonl"))

final_results = [] 

for single_case in all_requirements.values():
    tests = single_case.get("tests", [])
    basic_tests = [i for i in tests if i.get("test_level", "") == "冒烟"]
    extend_tests = [i for i in tests if i.get("test_level", "") != "冒烟"]
    
    basic_test_embeddings = []
    for test in basic_tests:
        basic_test_embeddings.append(cal_content_embedding(
            json.dumps(test, ensure_ascii=False, indent=2)
        ).tolist())

    new_result = {
        "case_id": single_case.get("case_id", ""),
        "case_name": single_case.get("case_name", ""),
        "requirement": single_case.get("requirement", ""),
        "initial_state": single_case.get("initial_state", ""),
        "trigger_condition": single_case.get("trigger_condition", ""),
        "action": single_case.get("action", ""),
        "basic_tests": basic_tests,
        "extend_tests": extend_tests,
        "requirement_embedding": single_case.get("embedding", []),
        "basic_test_embeddings": basic_test_embeddings,
    }
    final_results.append(new_result)


with open(os.path.join(PREPROCESSED_DATA_DIR, "all_test_cases_detailed.jsonl"), 'w', encoding='utf-8') as f:
    for item in final_results:
        f.write(json.dumps(item, ensure_ascii=False) + '\n')